<template>
  <div id="goTop">
    <div class="goTop" v-show="goTopShow" @click="goTop">
      <img src="../../../static/img/bossVersion/top.png">
    </div>
  </div>
</template>

<script>
import $ from "jquery";
export default {
  name: "goTop",
  data() {
    return {
      scrollTop: "",
      goTopShow: false
    };
  },
  methods: {
    handleScroll() {
      var box = $("#myScrollContent")[0];
      this.scrollTop =
        window.pageYOffset ||
        document.documentElement.scrollTop ||
        box.scrollTop;
      // debugger;
      if (this.scrollTop > 200) {
        this.goTopShow = true;
      } else {
        this.goTopShow = false;
      }
    },
    goTop() {
      var box = $("#myScrollContent")[0];
      let timer = null;
      const _that = this;
      cancelAnimationFrame(timer);
      timer = requestAnimationFrame(function fn() {
        if (_that.scrollTop > 0) {
          _that.scrollTop -= 50;
          box.scrollTop = document.documentElement.scrollTop = _that.scrollTop;
          timer = requestAnimationFrame(fn);
        } else {
          cancelAnimationFrame(timer);
          _that.goTopShow = false;
        }
      });
    }
  },
  mounted() {
    var box = $("#myScrollContent")[0];
    box.addEventListener("scroll", this.handleScroll);
  },
  destroyed() {
    var box = $("#myScrollContent")[0];
    box.removeEventListener("scroll", this.handleScroll);
  }
};
</script>

<style scoped>
#goTop {
  position: fixed;
  right: 0.1rem;
  bottom: 0.4rem;
  z-index: 10;
  width: 0.4rem;
  height: 0.4rem;
}
</style>